from flask import Flask

from flask_socketio import SocketIO,send,emit

app = Flask(__name__)

app.config['SECRET_KEY'] = 'secret!'

socketio = SocketIO(app)
socketio.init_app(app,cors_allowed_origins='*')

@socketio.on('message')

def handle_message(message):

    print('message')

    send(message)

# emit('my response')

@socketio.on('json')

def handle_json(json):

    print ('json')

# send(json, json=True)

@socketio.on('my event')

def handle_my_custom_event(json):
    print(json)

# emit('my response')

# send({'data': 'Connected'},json=True)

@socketio.on('connect', namespace='/chat')

def test_connect():

    emit('my response', {'data': 'Connected'})

@socketio.on('disconnect', namespace='/chat')

def test_disconnect():

    print('Client disconnected')

if __name__ == '__main__':

    socketio.run(app,debug=True,host='0.0.0.0',port=9999)